package com.cuihanyu.newuniversityover.activity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cuihanyu.newuniversityover.R;
import com.cuihanyu.newuniversityover.sql.DBManger;
import com.cuihanyu.newuniversityover.sql.MySQLiteHelper;
import com.cuihanyu.newuniversityover.sql.SQLiteUtil;
import com.cuihanyu.newuniversityover.util.MyLogUtil;

import java.sql.Date;
import java.text.SimpleDateFormat;

/**
 * Created by cuihanyu on 2018/3/23.
 */

public class StudyNoteActivity extends AppCompatActivity implements View.OnClickListener {
    private MySQLiteHelper helper;
    private SQLiteDatabase db;
    private Intent intent;
    private TextView mChapter_textView, mChapter_small, mOk_TextView, mTitle;
    private EditText mNoteBook_editText;
    private ImageView mBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_studynote);
        intent = getIntent();
        initView();
    }

    private void initView() {
        mBack = findViewById(R.id.back_ImageView);
        mBack.setOnClickListener(this);
        mTitle = findViewById(R.id.title_tv);
        mOk_TextView = findViewById(R.id.ok_TextView);
        mOk_TextView.setVisibility(View.VISIBLE);
        mOk_TextView.setOnClickListener(this);
        mChapter_textView = findViewById(R.id.Chapter_TextView);
        mChapter_small = findViewById(R.id.Chapter_Small);
        mNoteBook_editText = findViewById(R.id.NoteBook);
        initData();
    }

    private void initData() {
        mChapter_textView.setText(intent.getStringExtra("Chapter"));
        mChapter_small.setText(intent.getStringExtra("item"));
        mTitle.setText("笔记本");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_ImageView:
                finish();
                break;
            case R.id.ok_TextView:
                if (!mChapter_textView.getText().toString().isEmpty() &&
                        !mChapter_small.getText().toString().isEmpty() &&
                        !mNoteBook_editText.getText().toString().isEmpty()) {
                    helper = DBManger.getIntance(this);
                    db = helper.getWritableDatabase();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
                    Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                    String str = formatter.format(curDate);
                    String sql = "insert into " + SQLiteUtil.TABLE_NAME + " values(null,'" + str + "','"
                            + mChapter_textView.getText().toString() + "','" +
                            mChapter_small.getText().toString() + "','" + mNoteBook_editText.getText().toString() + "')";
                    MyLogUtil.e("11111sql", sql);
                    DBManger.execSQL(db, sql);
                    db.close();
                    finish();
                } else {
                    Toast.makeText(this, "请进行正确操作，填写正确信息", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }
}
